package com.iflysse.services.impl;

import com.iflysse.common.ResultEnum;
import com.iflysse.dao.ChapterDao;
import com.iflysse.dao.CourseDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.AddChapterBoReq;
import com.iflysse.pojo.bo.req.ChapterIdBoReq;
import com.iflysse.pojo.bo.req.CourseIdBoReq;
import com.iflysse.pojo.bo.resp.ChapterBoResp;
import com.iflysse.pojo.bo.resp.ChapterListBoResp;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.ChapterIdPoReq;
import com.iflysse.pojo.po.req.ChapterPoReq;
import com.iflysse.pojo.po.req.CourseIdPoReq;
import com.iflysse.pojo.po.resp.ChapterListPoResp;
import com.iflysse.services.ChapterService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;

@Service
public class ChapterServiceImpl implements ChapterService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);
    @Autowired
    ChapterDao chapterDao ;

    @Autowired
    CourseDao courseDao ;
    /**
     * 添加章
     *
     * @param addChapterBoReq 章
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp addChapter(AddChapterBoReq addChapterBoReq) throws SQLException {
        //dao执行的结果
        int result = 0;
        CommonPoResp commonPoResp = new CommonPoResp();
            //bo请求对象转po请求对象
            ChapterPoReq chapterPoReq = CommonUtil.objToObj(addChapterBoReq, ChapterPoReq.class);
            int update = chapterDao.addChapter(chapterPoReq);
            CommonUtil.printToPoResult(commonPoResp, update);
            CourseIdPoReq courseIdPoReq = new CourseIdPoReq();
            courseIdPoReq.setCourseId(addChapterBoReq.getCourseId());
            //更新课程的章节数量
            int amount = chapterDao.getAmountOfChapterByCourseId(courseIdPoReq);

            courseDao.updateChapterAmountByCourseId(courseIdPoReq, amount);
            //判断是否添加章和修改课程章数量成功
            if(commonPoResp.getCode() == ResultEnum.SUCCESS.getCode() && amount >0){
                result = 1;
            }

        //po响应转bo响应
        CommonBoResp commonBoResp = new CommonBoResp();
        CommonUtil.printToBoResult(commonBoResp,result);
        return commonBoResp;
    }

    /**
     * 根据课程获取章列表
     *
     * @param courseIdBoReq
     * @return
     */
    @Override
    public ChapterListBoResp loadChapterByCourseId(CourseIdBoReq courseIdBoReq) throws SQLException {
        CourseIdPoReq courseIdPoReq = CommonUtil.objToObj(courseIdBoReq, CourseIdPoReq.class);
        ChapterListPoResp chapterListByCourceId = chapterDao.getChapterListByCourseId(courseIdPoReq);
        ChapterListBoResp chapterListBoResp = new ChapterListBoResp();
        chapterListBoResp.setChapterBoRespList(new ArrayList<>());
        chapterListByCourceId.getChapterBoRespList().forEach(chapterPoResp -> {
            ChapterBoResp chapterBoResp = CommonUtil.objToObj(chapterPoResp, ChapterBoResp.class);
            chapterListBoResp.getChapterBoRespList().add(chapterBoResp);

        });

        return chapterListBoResp;
    }

    /**
     * 根据章编号删除章
     *
     * @param chapterIdBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp deleteChapterByChapterId(ChapterIdBoReq chapterIdBoReq) throws SQLException {
        ChapterIdPoReq chapterIdPoReq = CommonUtil.objToObj(chapterIdBoReq, ChapterIdPoReq.class);
        //根据章编号获取课程编号
        String courseId = chapterDao.getCourseIdByChapterId(chapterIdPoReq);

        //判断当前章所属课程是否已经发布,如果发布,无法删除
        CourseIdPoReq courseIdPoReq = new CourseIdPoReq();
        courseIdPoReq.setCourseId(courseId);
        //获取所属课程发布状态
        int publishState = courseDao.getPublishStateByCourseId(courseIdPoReq);

        CommonBoResp commonBoResp = new CommonBoResp();
        //判断是否发布
        if(publishState != 0){
            commonBoResp.setCode(ResultEnum.HASEXISTED.getCode());
            commonBoResp.setMsg("该课程已经发布,无法删除!");
            return commonBoResp;
        }
        int result = 0;
            int i = chapterDao.deleteChapterByChapterId(chapterIdPoReq);
            //更新课程的章节数量
            int amount = chapterDao.getAmountOfChapterByCourseId(courseIdPoReq);
            CommonPoResp commonPoResp =new CommonPoResp();
            int update = courseDao.updateChapterAmountByCourseId(courseIdPoReq, amount);
            CommonUtil.printToPoResult(commonPoResp, update);

            //判断删除和更新是否成功
            if (i > 0 && commonPoResp.getCode() == ResultEnum.SUCCESS.getCode()) {
                result = 1;
            }

        CommonUtil.printToBoResult(commonBoResp,result);
        return commonBoResp;
    }


}
